<?php
/**
 * -----------------------------------------------------
 *
 * models Member_model
 *
 * @package     models
 * @subpackage  
 * @category    Member_model  -  会员模型类
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Member_model extends CI_Model
{    
	public static $error;
	const TABLE = 'xc_members'; // 表名

	public function __construct()
	{
		parent::__construct();
        $this->load->database();
	}

	/**
	 * 增加数据
	 */
	public function insert($data)
	{
		if($this->db->insert(self::TABLE,$data)){
			return $this->db->insert_id();
		}else{
			return false;
		}
	}

             
	/**
	 * 查询所有的数据，并且分页
	 */
	public function get_all_data_and_page($limit)
	{
		$sql = 'select * from '.self::TABLE.' order by id desc '.$limit;
		$query=$this->db->query($sql);
		if($query->num_rows()){
			$arr = $query->result_array();
			foreach ($arr as $key => $val) {
				$arr[$key]['addinfo'] = $this->get_region_name($val['province']).'-'.$this->get_region_name($val['city']).'-'.$this->get_region_name($val['area']);
				$arr[$key]['indname'] = $this->get_industry_name($val['industry']);
			}
			return $arr;
		}
	}


	/**
	 * 获取地区名称
	 */
	public function get_region_name($id){
		$sql = "select id,name from xc_delivery_region where id=".$id;
		$query = $this->db->query($sql);
		if($query->num_rows() > 0){
			$arr = $query->row_array();
			return $arr['name'];
		}	
	}

	/**
	 * 获取行业名称
	 */
	public function get_industry_name($id){
		$sql = "select id,name from xc_industry where id=".$id;
		$query = $this->db->query($sql);
		if($query->num_rows() > 0){
			$arr = $query->row_array();
			return $arr['name'];
		}	
	}

	/**
	 * 查询共有多少数据
	 */
	public function get_num_rows()
	{
		$query = $this->db->get(self::TABLE);
		return $query->num_rows();
	}

	/**
	 * [get_data_by_mobile 根据手机号码查询手机条数]
	 * @param  [type] $mobile [description]
	 * @return [type]         [description]
	 */
	public function get_data_by_mobile($mobile){
		$this->db->where('mobile',$mobile);
		$query = $this->db->get(self::TABLE);
		return $query->num_rows();
	}
	/**
	 * [get_data_by_mobile 根据手机号码查询会员信息]
	 * @param  [type] $mobile [description]
	 * @return [type]         [description]
	 */
	public function get_a_member_by_mobile($mobile){
		$this->db->where('mobile',$mobile);
		$query = $this->db->get(self::TABLE);
		return $query->row_array();
	}

	/**
	 * [get_a_member_by_mid 根据会员ID查询会员信息]
	 * @param  [type] $mobile [description]
	 * @return [type]         [description]
	 */
	public function get_a_member_by_mid($mid){
		$this->db->where('id',$mid);
		$query = $this->db->get(self::TABLE);
		return $query->row_array();
	}

	/**
	 * 编辑数据信息
	 */
	public function update($data,$id)
	{
		if($this->db->update(self::TABLE,$data,'id = '.$id)){
			return true;
		}else{
			return false;
		}
	}


	
}